#!/usr/bin/env python
# Function to pull the latest NWS forecast

def scrub_nws(WFO,site_name,siteid):
    # Read the html from the page
    import urllib, re, sys
    sys.path.append('..')
    from INFILE import maindir

    base_page = 'http://forecast.weather.gov/product.php?site=NWS&issuedby=%s&product=PFM&format=txt&version=1&glossary=0' % WFO
    infile = urllib.urlopen(base_page)
    info = infile.read().split('$$')
    infile.close()
    for block in info:
        if re.search(site_name,block):
            print "Site found!"
            lines = block.split('\n')
            block_start = False
            block_end = False
            fcst_dict = {}
            for line in lines:
                # Look for the appropriate info.  Just need a few lines
                if len(line.split()) > 0:
                    if line.split()[0] == 'DATE':
                        block_start = True  
                    if line.split()[0] == 'SNOW':
                        block_end = True
                    if line.split()[0] == 'MAX' and line.split()[1] == 'HEAT':
                        block_end = True
                    if block_start and not block_end:
                        if line.split()[0] == 'WIND':
                            if line.split()[1] == 'SPD':
                                fcst_dict[line.split()[0]] = line.split()[1:]
                        else:
                            fcst_dict[line.split()[0]] = line.split()[1:]
    # For now just grab the data we need
    # Remove the '3hourly' from UTC
    fcst_dict['UTC'] = fcst_dict['UTC'][1:]
    #for keyval in fcst_dict.keys():
    #    print keyval,fcst_dict[keyval]
    # Find the index of 06Z
    try:
        start_idx = fcst_dict['UTC'].index('06')
    except:
        # Central region does things on central time...
        if '05' in fcst_dict['UTC']:
            #print fcst_dict['UTC']
            start_idx = fcst_dict['UTC'].index('05')
        elif '07' in fcst_dict['UTC']:
            start_idx = fcst_dict['UTC'].index('07')

    # We know there are 9 values in a day every three hours (inclusive)
    #print fcst_dict['WIND'][start_idx:start_idx+9] 
    try:
        windlist = [int(k) for k in fcst_dict['WIND'][start_idx:start_idx+9]]
    except:
        windlist = [int(k) for k in fcst_dict['WIND'][start_idx+1:start_idx+9]]


    #print windlist
    max_wind = float(max(windlist)) * 0.868976   # convert to knots


    # Guess at precip--the second two values.  Doesn't exactly match our time of
    # interest, but it's close.
    precip_val = float(fcst_dict['QPF'][2]) + float(fcst_dict['QPF'][3])

    # First two or three values should be next day's high and low
    # Where the date is depends on where the forecast is starting
    from datetime import datetime
    #print fcst_dict['DATE']
    if 'MIN/MAX' in fcst_dict.keys():
        high = fcst_dict['MIN/MAX'][1]
        low = fcst_dict['MIN/MAX'][0]
        # And the date of this forecast
        try:
            fcst_date = datetime.strptime(fcst_dict['DATE'][2],'%m/%d/%y')
        except:
            fcst_date = datetime.strptime(fcst_dict['DATE'][3],'%m/%d/%y')
    else:
        high = fcst_dict['MAX/MIN'][2]
        low = fcst_dict['MAX/MIN'][1]
        # And the date of this forecast
        fcst_date = datetime.strptime(fcst_dict['DATE'][3],'%m/%d/%y')
    print siteid, fcst_date.strftime('%Y-%m-%d'), high, low, max_wind, precip_val


    # Now try to export -- overwrite fcst_dict
    import os, cPickle
    if os.path.exists('%s/site_data/%s_NWS_fcst.pickle' % (maindir,siteid)):
        f = open('%s/site_data/%s_NWS_fcst.pickle' % (maindir,siteid),'r')
        fcst_dict = cPickle.load(f)
        f.close()
    else:
        fcst_dict = {}
    # Quick fix on QPF to make a new value
    #if 'precip' not in fcst_dict.keys():
    #    fcst_dict['precip'] = [0.0 for f in fcst_dict['highs']]

    if fcst_date not in fcst_dict.keys():
        fcst_dict[fcst_date] = {}
        fcst_dict[fcst_date]['high'] = float(high)
        fcst_dict[fcst_date]['low'] = float(low)
        fcst_dict[fcst_date]['wind'] = float(max_wind)
        fcst_dict[fcst_date]['precip'] = float(precip_val)
    else:
        print "Replacing forecast for %s with new values" % fcst_date.strftime('%m/%d/%Y')
        fcst_dict[fcst_date]['high'] = float(high)
        fcst_dict[fcst_date]['low'] = float(low)
        fcst_dict[fcst_date]['wind'] = float(max_wind)
        fcst_dict[fcst_date]['precip'] = float(precip_val)
           
    # Dump the resulting dictionary
    try:
        f = open('%s/site_data/%s_NWS_fcst.pickle' % (maindir,siteid), 'w')
        cPickle.dump(fcst_dict,f)
        f.close()
    except:
        print "Unable to save pickle file for site", siteid

try:
    scrub_nws('SEW','SEATTLE-KING WA','KSEA')
except:
    pass
try:
    scrub_nws('LOT','GREATER ROCKFORD AIRPORT-WINNEBAGO IL','KRFD')
except:
    pass
try:
    scrub_nws('MOB','PENSACOLA-ESCAMBIA FL','KPNS')
except:
    pass
try:
    scrub_nws('BYZ','BILLINGS-YELLOWSTONE MT','KBIL')
except:
    pass
try:
    scrub_nws('LMK','LOUISVILLE-JEFFERSON KY','KSDF')
except:
    pass
try:
    scrub_nws('pqr','ASTORIA-CLATSOP OR','KAST')
except:
    pass

try:
    scrub_nws('BGM','SYRACUSE HANCOCK INTERNATIONAL AIRPORT-ONONDAGA NY','KSYR')
except:
    pass
try:
    scrub_nws('EWX','AUSTIN BERGSTROM-TRAVIS TX','KAUS')
except:
    pass
try:
    scrub_nws('LOX','BURBANK CA','KBUR')
except:
    pass
try:
    scrub_nws('OKX','NEWARK-ESSEX NJ','KEWR')
except:
    pass
try:
    scrub_nws('DLH','DULUTH-ST. LOUIS MN','KDLH')
except:
    pass
try:
    scrub_nws('ICT','WICHITA-SEDGWICK','KICT')
except:
    pass

